package cn.school.job.Info;

import cn.school.prevention.dao.InfoDao;
import cn.school.prevention.entity.Info;
import cn.school.prevention.service.InfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
@DisallowConcurrentExecution
public class InfoQuartzJob extends QuartzJobBean {

    private static Logger logger = LoggerFactory.getLogger(InfoQuartzJob.class);

    @Autowired
    private InfoDao infoDao;
    @Autowired
    private InfoService infoService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        try {
            logger.info("InfoQuartz" + Calendar.getInstance().getTime().getTime());
            //查出info表的集合
            LambdaQueryWrapper<Info> wrapper = new LambdaQueryWrapper<>();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date(System.currentTimeMillis());
            String nowDate = formatter.format(date);
            wrapper.eq(Info::getStatus, "1");
            wrapper.le(Info::getBeginDate, nowDate);
            wrapper.ge(Info::getEndDate,nowDate);
            List<Info> infoList = infoDao.selectList(wrapper);
            for (Info info : infoList) {
             if (info.getClassId()!=null){
                String [] tag=info.getClassId().split(",");
                List<String>list= Arrays.asList(tag);
                info.setClassIds(list);
            }
                infoService.insert(info, true);
            }
        } catch (Exception e) {
            logger.error("", e);
        }

    }
}
